/*
  Copyright (c) 2009 Dave Gamble

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
#ifndef cJSON_TOOLS_h
#define cJSON_TOOLS_h

#include <vector>
#include <string>
#include <cstdlib>

#include "thirdparty/cjson/cJSON.h"

char *readFile(const char *filename);
void freeData(char * data);
int saveJsonFile(cJSON *root, const char *filename);
bool saveFile(const char *data, const char *filename);

std::vector<std::string> parseStringObject(cJSON *js);
std::vector<std::string> parseString(const std::string &str);

template<class T>
inline bool cJSON_GetInt(cJSON *root, const char *name, T &val)
{
    cJSON *js = cJSON_GetObjectItem(root, name);
    if (js) {
        if (js->type == cJSON_Number)
            val = static_cast<T>(js->valueint);
        else if (js->type == cJSON_True)
            val = static_cast<T>(1);
        else if (js->type == cJSON_False)
            val = static_cast<T>(0);
        else
            return false;
        return true;
    }
    else {
        return false;
    }
}

template<class T>
inline bool cJSON_GetFloat(cJSON *root, const char *name, T &val)
{
    cJSON *js = cJSON_GetObjectItem(root, name);
    if (js && js->type == cJSON_Number) {
        val = static_cast<T>(js->valuedouble);
        return true;
    }
    else {
        return false;
    }
}

inline bool cJSON_GetString(cJSON *root, const char *name, std::string &val)
{
    cJSON *js = cJSON_GetObjectItem(root, name);
    if (js && js->type == cJSON_String) {
        val = js->valuestring;
        return true;
    }
    else {
        return false;
    }
}


template<class T>
inline bool cJSON_GetInt(cJSON *root, const char *name, T &val, cJSON *arr_params)
{
    if (cJSON_GetInt(root, name, val))
        return true;

    cJSON *js = cJSON_GetObjectItem(root, name);
    if (js && js->type == cJSON_String &&
        arr_params && arr_params->type==cJSON_Array)
    {
        std::string str = js->valuestring;
        if( str.size() ) {
            str.erase(0, str.find_first_not_of(" "));
            str.erase(str.find_last_not_of(" ") + 1);
        }

        std::string::size_type pos = str.find('$');
        if(pos == std::string::npos) {
            return false;
        }
        str.erase(str.begin(), str.begin()+pos+1);

        int item = atoi(str.c_str());
        cJSON * js_item = cJSON_GetArrayItem(arr_params, item);
        if (js_item && js_item->type == cJSON_Number) {
            val = static_cast<T>(js_item->valueint);
            return true;
        }
    }
    return false;
}

template<class T>
inline bool cJSON_GetFloat(cJSON *root, const char *name, T &val, cJSON *arr_params)
{
    if (cJSON_GetInt(root, name, val))
        return true;

    cJSON *js = cJSON_GetObjectItem(root, name);
    if (js && js->type == cJSON_String &&
        arr_params && arr_params->type==cJSON_Array)
    {
        std::string str = js->valuestring;
        if( str.size() ) {
            str.erase(0, str.find_first_not_of(" "));
            str.erase(str.find_last_not_of(" ") + 1);
        }

        std::string::size_type pos = str.find('$');
        if(pos == std::string::npos) {
            return false;
        }
        str.erase(str.begin(), str.begin()+pos+1);

        int item = atoi(str.c_str());
        cJSON * js_item = cJSON_GetArrayItem(arr_params, item);
        if (js_item && js_item->type == cJSON_Number) {
            val = static_cast<T>(js_item->valuedouble);
            return true;
        }
    }
    return false;
}

inline bool cJSON_GetString(cJSON *root, const char *name, std::string &val, cJSON *arr_params)
{
    if (!cJSON_GetString(root, name, val))
        return false;

    if( val.size() ) {
        val.erase(0, val.find_first_not_of(" "));
        val.erase(val.find_last_not_of(" ") + 1);
    }
    std::string::size_type pos = val.find('$');
    if (pos != std::string::npos) {
        if (arr_params && arr_params->type==cJSON_Array)
        {
            val.erase(val.begin(), val.begin()+pos+1);
            int item = atoi(val.c_str());
            cJSON * js_item = cJSON_GetArrayItem(arr_params, item);
            if (js_item && js_item->type == cJSON_String) {
                val = js_item->valuestring;
                return true;
            }
        }
        return false;
    }

    return true;
}

template<class T>
inline bool cJSON_GetArray(cJSON *root, const char *name, T &val)
{
    cJSON *js = cJSON_GetObjectItem(root, name);
    if (js) {
        if (js->type == cJSON_Number)
            val = static_cast<T>(js->valueint);
        else if (js->type == cJSON_True)
            val = static_cast<T>(1);
        else if (js->type == cJSON_False)
            val = static_cast<T>(0);
        else
            return false;
        return true;
    }
    else {
        return false;
    }
}


template <class T, class Ts>
inline bool cJSON_ParseArray(cJSON *root, const char * name, Ts &infos,
                             cJSON *arr_params, bool (*parseItem)(cJSON *, T&, cJSON *))
{
    cJSON *js = cJSON_GetObjectItem(root, name);
    if (!js) return true;

    if (js->type == cJSON_Array) {
        int n = 0;
        do {
            cJSON* arrItem = cJSON_GetArrayItem(js, n);
            if (arrItem) {
                T*  item = new T();
                if (parseItem(arrItem, *item, arr_params))
                    infos.push_back(item);
                else
                    goto error;
                n++;
            }
            else
                break;
        } while(true);
    }

    return true;
error:
    return false;
}

#endif
